package com.code.demo.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;

import butterknife.BindView;

/**
 * Created by yangliguo on 2019/3/2.
 */

public class MyService extends Service {

    private MyBinder binder = new MyBinder();

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("TagService", "MyService onCreate");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("TagService", "MyService onDestroy");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int result = super.onStartCommand(intent, flags, startId);
        Log.e("TagService", "MyService onStartCommand result = " + result);
        return result;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    public class MyBinder extends Binder {

        int i = 0;

        public void startCount() {
            Log.e("TagService", "MyService startCount");
            new Thread(new Runnable() {
                @Override
                public void run() {
                    do {
                        try {
                            Thread.sleep(500);
                            Log.e("TagService", "MyService Runnable i = " + ++i);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } while (i<100);
                }
            }).start();
        }

        public int getCount() {
            Log.e("TagService", "MyService getCount i = " + i);
            stopSelf();
            return i;
        }
    }
}
